<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>
            </div>

            <!-- 搜索表单 -->
            <el-form :model="query" ref="searchRuleForm" label-width="100px">
                <el-row>
                    <el-col :sm="20" :md="6">
                        <el-form-item label="角色名:" prop="roleName">
                            <el-input v-model="query.roleName" placeholder="请输入" clearable></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :sm="20" :md="6" align="center">
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            @click="submitForm('searchRuleForm')"
                        >搜索</el-button>
                        <el-button type="info" @click="resetForm('searchRuleForm')">重置</el-button>
                    </el-col>
                </el-row>
            </el-form>

            <div class="tree-box">
                <div class="table-box">
                    <table-List
                        ref="tableList"
                        :tableData="tableData"
                        :tableHeader="tableHeader"
                        :loading="loading"
                        :pageShow="false"
                        :pagination="pagination"
                        @pageChange="pageChange"
                        @selectFun="selectFun"
                        @sortChange="sortChange"
                    ></table-List>
                </div>
            </div>
        </div>
        <!--  -->
        <el-dialog title="角色" :visible.sync="dialogFormVisible" destroy-on-close>
            <el-form :model="formData" :rules="rules" ref="dialogForm" label-width="100px">
                <el-form-item label="角色名称:" prop="roleName">
                    <el-input v-model="formData.roleName" clearable></el-input>
                </el-form-item>
                <el-form-item label="角色备注:" prop="roleRemark">
                    <el-input
                        type="textarea"
                        maxlength="300"
                        show-word-limit
                        v-model="formData.roleRemark"
                    ></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm('dialogForm')">取 消</el-button>
                <el-button type="primary" @click="submitForm('dialogForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
var _ = require('lodash');
export default {
    name: 'roleList',
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                roleName: '' //
            },
            rules: {
                roleName: [
                    { required: true, message: '请输入', trigger: 'change' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'change' }
                ]
            },
            formData: {
                roleName: '',
                roleRemark: ''
            },
            dialogFormVisible: false,
            // 通用表格
            loading: false,
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 0
            },
            tableData: [
                {
                    id: '', //角色ID
                    roleName: '', //角色名字
                    roleRemark: '', //角色备注
                    updateTime: '', //修改时间
                    updateUserId: '', //修改人id
                    updateUserName: '' //修改用户
                }
            ],
            tableHeader: [
                // 表头数据
                { prop: 'roleName', label: '角色名', minWidth: '100px' },
                {
                    prop: 'roleRemark',
                    label: '角色备注'
                },
                {
                    prop: 'updateUserName',
                    label: '最后修改用户'
                },
                {
                    prop: 'updateTime',
                    label: '最后修改时间',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM-dd hh:mm:ss');
                    }
                },
                { prop: 'active', active: [{ name: '权限编辑', router: 'rolepermissionsrel', routerId: '' }] }
            ],
            multipleSelection: []
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
        // console.log(this.$router, 'router')
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.loading = true;

            appMain
                .roleFindAllPageByDTO(this.query)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.timer = setTimeout(() => {
                            //设置延迟执行
                            this.loading = false;
                        }, 1000);

                        this.tableData = res.data.data;
                        console.log('getData tableData', this.tableData);
                    } else {
                        this.loading = false;
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    this.loading = false;
                    console.log(err);
                });
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                })
                .catch(() => {});
        },
        deleteById(id) {
            appMain
                .roleDel({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.getData();
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        getById(id) {
            appMain
                .roleFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.form = res.data.data;
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        updateById() {
            appMain
                .roleUpdateById(this.formData)
                .then(res => {
                    if (res.data.code === 20000) {
                        //this.form = res.data.data;
                        this.dialogFormVisible = false;
                        this.getData();
                        this.resetForm('dialogForm');
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        addRole() {
            console.log('addRole', this.formData);
            appMain
                .roleInsert(this.formData)
                .then(res => {
                    if (res.data.code === 20000) {
                        //this.form = res.data.data;
                        this.dialogFormVisible = false;
                        this.getData();
                        this.resetForm('dialogForm');
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }

            this.formData = _.cloneDeep(this.multipleSelection[0]);
            this.dialogFormVisible = true;
        },
        goAdd() {
            this.dialogFormVisible = true;
        },
        resetForm(formName) {
            console.log('resetForm', this.$refs[formName].resetFields());
            this.$refs[formName].resetFields();
            this.dialogFormVisible = false;
            this.formData = {
                roleName: '',
                roleRemark: ''
            };
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    if (formName === 'searchRuleForm') {
                        this.getData();
                    } else if (formName === 'dialogForm') {
                        if (this.formData.id) {
                            //更新
                            this.updateById();
                        } else {
                            //新增
                            this.addRole();
                        }
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        //表格
        // 分页导航
        pageChange(data) {},
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //表格组件返回排序对象
        sortChange(data) {}
    }
};
</script>


<style lang="css" scoped>
.container {
    box-sizing: border-box;
}
.handle-box {
    margin-bottom: 20px;
    flex-wrap: wrap;
    font-size: 12px;
}
.handle-box > div,
.handle-box > button {
    margin-bottom: 15px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 120px;
    display: inline-block;
}
.tree-box {
    border: 1px solid #ddd;
    margin-top: 20px;
    height: calc(100% - 146px);
}
.table-box {
    height: calc(100% - 72px);
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.ml10 {
    margin-left: 10px;
}
</style>